import {root, StaticViewGroup, subscribe, handler} from "adajs";
import Status from "./status";
import "./style/reset.scss";
import dispatcher from "./lib/socket";
import Loading from "ada-uikit/src/loading";

@root()
class Root extends StaticViewGroup {
	oncreated() {
		dispatcher.observe(this);
		this.addChild(Status);
	}

	@handler("startask")
	startTask() {
		this.addChild(Loading, {name: "loading"});
	}

	@subscribe("status")
	status(data) {
		this.removeChildByName("loading");
		if (data.done !== undefined) {
			if (data.done === true) {
				this.addChild(Loading).then(loading => {
					loading.showSuccess("Run Task Done");
					loading.close(4000);
				});
			} else {
				this.addChild(Loading).then(loading => {
					loading.showError("Run Task Error");
					loading.close(4000);
				});
			}
		}
	}
}

export default Root;